package com.lin.utils.treeuntil;

import com.lin.dataobject.Category;
import com.lin.utils.treenode.CategoryTreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/2.
 */
public class CategoryTreeUntil {
    public List fillTree(List<Category> institutions){
        List<CategoryTreeNode> list=new ArrayList<CategoryTreeNode>();
        Map map=new HashMap<String, CategoryTreeNode>();
        try {
            for(Category institution:institutions){
                CategoryTreeNode node=new CategoryTreeNode();
                node.setId(institution.getId());
                node.setText(institution.getName());
                node.setRemark(institution.getRemark());
                if(institution.getPid()!=0){
                    node.setPid(institution.getPid());
                }
                map.put(institution.getId(), node);

            }

            for(Category institution:institutions){
                if(institution.getPid()!=0){
                    if(institution.getPid()==0){

                        list.add((CategoryTreeNode)map.get(institution.getId()));
                    }else{
                        Integer pId=institution.getPid();
                        CategoryTreeNode pNode=(CategoryTreeNode)map.get(pId);
                        CategoryTreeNode cNode=(CategoryTreeNode)map.get(institution.getId());
                        pNode.addChild(cNode);
                    }
                }else {
                    list.add((CategoryTreeNode)map.get(institution.getId()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
